package cn.jobstart.spider;

import cn.jobstart.spider.listener.ISkSpiderListener;
import cn.jobstart.spider.listener.SkSpiderListener;
import cn.jobstart.spider.spider.AbstractSpider;

import javax.management.JMException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description 爬虫监听
 * @date 2023-09-20 09:37
 */
public class SkSpiderMonitor {


    private static final SkSpiderMonitor INSTANCE = new SkSpiderMonitor();

    private final List<SkSpiderStatus> spiderStatuses = new ArrayList<SkSpiderStatus>();

    private SkSpiderMonitor() {

    }

    public static SkSpiderMonitor instance() {
        return INSTANCE;
    }

    public synchronized SkSpiderMonitor register(AbstractSpider... spiders) throws JMException {
        for (AbstractSpider spider : spiders) {
            ISkSpiderListener monitorSpiderListener = new SkSpiderListener(spider.getSpiderMonitor());
            spider.addListener(monitorSpiderListener);
            SkSpiderStatus spiderStatus = getSpiderStatus(spider, monitorSpiderListener);
            spiderStatuses.add(spiderStatus);
        }
        return this;


    }

    protected SkSpiderStatus getSpiderStatus(AbstractSpider spider, ISkSpiderListener monitorSpiderListener) {
        return new SkSpiderStatus(spider, monitorSpiderListener);
    }


}
